package com.cwj.film.AOP;


import com.cwj.common.Beans.film.requestBeans;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 没有使用
 */
@Component
@Aspect
@Order(0)
public  class IoStream {
    @Resource
    private HttpServletRequest request;
    @Pointcut("execution(* com.cwj.film.service.inter.about.*(..) )")
    private void pointcut(){}
    @Before("pointcut()")
    private void createCloseableList(){
        requestBeans requestBeans = new requestBeans();
        requestBeans.setCloseableList(new ArrayList<>());
        request.setAttribute("requestBeans",requestBeans);
    }
    //*********************************************

   @After("pointcut()")
   public void closeCloseableList() {
       List<Closeable>  closeableList = ((requestBeans)request.getAttribute("requestBeans")).getCloseableList();

       if (closeableList == null || closeableList.size() <= 0) {
           assert closeableList != null;
           for (Closeable closeable : closeableList) {
               try {
                   closeable.close();
               } catch (IOException ignored) {

               }
           }
       }
   }


}
